using Microsoft.EntityFrameworkCore;
using JGSY.CMS.LowCode.Platform.Domain.Entities;
using JGSY.CMS.LowCode.Platform.Domain.Interfaces;
using JGSY.CMS.LowCode.Platform.Infrastructure.CmsDbContext;

namespace JGSY.CMS.LowCode.Platform.Infrastructure.Repositories
{
    /// <summary>
    /// 模板仓储实现
    /// </summary>
    public class TemplateRepository : Repository<Template>, ITemplateRepository
    {
        public TemplateRepository(LowCodeDbContext context) : base(context)
        {
        }

        /// <summary>
        /// 根据类型获取模板列表
        /// </summary>
        public async Task<List<Template>> GetByTypeAsync(string type)
        {
            return await _context.Templates
                .Where(x => x.Type == type && x.Status == 1)
                .OrderBy(x => x.Name)
                .ToListAsync();
        }

        /// <summary>
        /// 根据分类获取模板列表（Template 实体没有 Category 属性，使用 Type 替代）
        /// </summary>
        public async Task<List<Template>> GetByCategoryAsync(string category)
        {
            return await _context.Templates
                .Where(x => x.Type == category && x.Status == 1)
                .OrderBy(x => x.Name)
                .ToListAsync();
        }

        /// <summary>
        /// 根据名称获取模板
        /// </summary>
        public async Task<Template?> GetByNameAsync(string name)
        {
            return await _context.Templates
                .FirstOrDefaultAsync(x => x.Name == name && x.Status == 1);
        }

        /// <summary>
        /// 获取默认模板（Template 实体没有 IsDefault 属性，返回第一个启用的模板）
        /// </summary>
        public async Task<Template?> GetDefaultAsync(string type)
        {
            return await _context.Templates
                .Where(x => x.Type == type && x.Status == 1)
                .OrderBy(x => x.Id)
                .FirstOrDefaultAsync();
        }

        /// <summary>
        /// 检查模板名称是否存在
        /// </summary>
        public async Task<bool> ExistsAsync(string name, Guid? excludeId = null)
        {
            var query = _context.Templates.Where(x => x.Name == name);
            
            if (excludeId.HasValue)
            {
                // 将 Guid 转换为 long 进行比较（简化处理）
                var longId = excludeId.Value.GetHashCode();
                query = query.Where(x => x.Id != longId);
            }
            
            return await query.AnyAsync();
        }

        /// <summary>
        /// 获取启用的模板列表（使用 Status 替代 IsEnabled）
        /// </summary>
        public async Task<List<Template>> GetEnabledTemplatesAsync(string? type = null)
        {
            var query = _context.Templates.Where(x => x.Status == 1);
            
            if (!string.IsNullOrEmpty(type))
            {
                query = query.Where(x => x.Type == type);
            }
            
            return await query.OrderBy(x => x.Name).ToListAsync();
        }

        /// <summary>
        /// 获取系统模板列表（Template 实体没有 IsSystem 属性，返回所有启用模板）
        /// </summary>
        public async Task<List<Template>> GetSystemTemplatesAsync()
        {
            return await _context.Templates
                .Where(x => x.Status == 1)
                .OrderBy(x => x.Name)
                .ToListAsync();
        }

        /// <summary>
        /// 获取最近使用的模板列表（Template 实体没有 LastUsedAt 属性，按更新时间排序）
        /// </summary>
        public async Task<List<Template>> GetRecentlyUsedAsync(int count = 10)
        {
            return await _context.Templates
                .Where(x => x.Status == 1)
                .OrderByDescending(x => x.UpdatedAt)
                .Take(count)
                .ToListAsync();
        }
    }
}
